package com.tnovoselec.android.favorites.entity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import com.tnovoselec.android.favorites.enums.MimeTypeEnum;

/**
 * Title: Android Framework - Favorites <br />
 * <br />
 * Copyright: Copyright (c) 2011 <br />
 * <br />
 * NTH Company: NTH d.o.o. <br />
 * <br />
 * 
 * NewsFavorites object holds all the data about one news detail.
 * 
 * @author Tomislav Novoselec
 * @version 1.0.0
 * @since 1.0.0
 */

@DatabaseTable(tableName = "favorites")
public class NewsFavoritesDto extends FavoritesDto {

    @DatabaseField(id = true, canBeNull = false, dataType = DataType.INTEGER)
    private Integer id = null;

    @DatabaseField(canBeNull = true, dataType = DataType.STRING)
    private String title;

    @DatabaseField(canBeNull = true, dataType = DataType.STRING)
    private String description;

    private String imageUrl;

    @DatabaseField(canBeNull = true, dataType = DataType.STRING)
    private String link;

    @DatabaseField(canBeNull = true, dataType = DataType.STRING)
    private String guid;

    @DatabaseField(canBeNull = true, dataType = DataType.STRING)
    private String author;

    @DatabaseField(canBeNull = true, dataType = DataType.STRING)
    private String lastBuildDate;

    @DatabaseField(canBeNull = true, dataType = DataType.STRING)
    private String pubDate;

    @DatabaseField(canBeNull = true, dataType = DataType.STRING)
    private String content;

    @DatabaseField(canBeNull = true, dataType = DataType.STRING)
    private String commentsLink;

    @DatabaseField(canBeNull = false, dataType = DataType.STRING)
    private String parentChannelId;

    private long pubTime;
    private List<EnclosureFavoritesDto> enclosures;
    private Map<MimeTypeEnum, List<EnclosureFavoritesDto>> files;

    public NewsFavoritesDto() {
	super();
	enclosures = new ArrayList<EnclosureFavoritesDto>();
	files = new HashMap<MimeTypeEnum, List<EnclosureFavoritesDto>>();
    }

    public int getId() {
	return new Integer(super.getId());
    }

    public void setId(int id) {
	this.id = id;
    }

    public String getTitle() {
	return title;
    }

    public void setTitle(String title) {
	this.title = title;
    }

    public String getDescription() {
	return description;
    }

    public void setDescription(String description) {
	this.description = description;
    }

    public String getImageUrl() {
	return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
	this.imageUrl = imageUrl;
    }

    public String getLink() {
	return link;
    }

    public void setLink(String link) {
	this.link = link;
    }

    public String getGuid() {
	return guid;
    }

    public void setGuid(String guid) {
	this.guid = guid;
    }

    public String getAuthor() {
	return author;
    }

    public void setAuthor(String author) {
	this.author = author;
    }

    public String getLastBuildDate() {
	return lastBuildDate;
    }

    public void setLastBuildDate(String lastBuildDate) {
	this.lastBuildDate = lastBuildDate;
    }

    public String getPubDate() {
	return pubDate;
    }

    public void setPubDate(String pubDate) {
	this.pubDate = pubDate;
	try {
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    Date date = sdf.parse(pubDate);
	    setPubTime(date.getTime());
	} catch (Exception e) {
	}
    }

    public String getContent() {
	return content;
    }

    public void setContent(String content) {
	this.content = content;
    }

    public String getCommentsLink() {
	return commentsLink;
    }

    public void setCommentsLink(String commentsLink) {
	this.commentsLink = commentsLink;
    }

    public String getParentChannelId() {
	return parentChannelId;
    }

    public long getPubTime() {
	return pubTime;
    }

    public void setPubTime(long pubTime) {
	this.pubTime = pubTime;
    }

    public void setParentChannelId(String parentChannelId) {
	this.parentChannelId = parentChannelId;
    }

    public List<EnclosureFavoritesDto> getEnclosures() {
	return enclosures;
    }

    public void setEnclosures(List<EnclosureFavoritesDto> enclosures) {
	this.enclosures = enclosures;
    }

    public void addEnclosure(EnclosureFavoritesDto enclosure) {
	this.enclosures.add(enclosure);
    }

    public void setId(Integer id) {
	this.id = id;
    }

    public List<EnclosureFavoritesDto> getEnclosuresByType(MimeTypeEnum type) {
	if (files.containsKey(type)) {
	    return files.get(type);
	}

	final ArrayList<EnclosureFavoritesDto> items = new ArrayList<EnclosureFavoritesDto>();
	for (EnclosureFavoritesDto item : enclosures) {
	    if (item.getMimeType() != null && item.getMimeType().matches(type.getRegex())) {
		items.add(item);
	    }
	}
	files.put(type, items);
	return items;
    }

    public String getThumb() {
	if (isImageGalleryAvailable()) {
	    return getEnclosuresByType(MimeTypeEnum.IMAGE).get(0).getProperty(EnclosureFavoritesDto.keyThumbUrl);
	}
	return null;
    }

    public String getFirstArticleImage() {
	if (isImageGalleryAvailable()) {
	    return getEnclosuresByType(MimeTypeEnum.IMAGE).get(0).getUrl();
	}
	return null;
    }

    public boolean isImageGalleryAvailable() {
	return !getEnclosuresByType(MimeTypeEnum.IMAGE).isEmpty();
    }

    public boolean isVideoGalleryAvailable() {
	return !getEnclosuresByType(MimeTypeEnum.VIDEO).isEmpty();
    }

    public static String getCreateQuery() {
	return "CREATE TABLE favorites (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "title TEXT, " + "description TEXT, " + "link TEXT, " + "guid TEXT, " + "author TEXT, " + "lastBuildDate TEXT, "
		+ "pubDate TEXT, " + "content TEXT, " + "commentsLink TEXT, " + "parentChannelId TEXT);";
    }

    public static String getTriggers() {
	return "CREATE TRIGGER deleteEnclosures BEFORE DELETE ON favorites FOR EACH ROW BEGIN DELETE FROM enclosures WHERE " + "enclosures.parentChannelId = OLD.guid; END";

    }

    @Override
    public boolean isValid() {
	// TODO Auto-generated method stub
	return true;
    }

}
